from datetime import date, datetime
from typing import Union

from fastapi import Query
from pydantic import BaseModel, Field

from forward.extend.pydantic.base import BaseSchema
from forward.schema_base import EmptyStrToNone
from forward.utils.tools import ToolsUtil


class ArticleCateOutSchema(BaseSchema):
    """
    文章分类输出
    """

    id: int
    name: str | None = None
    sort: int
    number: Union[int, None] = None
    is_show: int
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleCateListInSchema(BaseModel):
    """
    文章分类列表入参
    """

    name: Union[str, None] = Query(default=None)
    is_show: Union[int, None] = Query(default=None, alias="isShow")


class ArticleCateDetailInSchema(BaseModel):
    """
    文章分类详情入参
    """

    id: int = Query(gt=0)  # 主键


class ArticleCateAddInSchema(BaseModel):
    """
    文章分类新增入参
    """

    name: str
    sort: int = Field(ge=0)  # 排序
    is_show: int = Field(alias="isShow", ge=0, le=1)  # 是否显示: [0=否, 1=是]


class ArticleCateEditInSchema(ArticleCateAddInSchema):
    """
    文章分类修改入参
    """

    id: int = Query(gt=0)  # 主键


class ArticleCateDeleteInSchema(BaseModel):
    """
    文章分类删除入参
    """

    id: int = Query(gt=0)  # 主键


class ArticleCateChangeInSchema(BaseModel):
    """
    文章分类状态修改入参
    """

    id: int = Query(gt=0)  # 主键


class ArticleListInSchema(BaseModel):
    """
    文章列表
    """

    title: str = Query(default="")
    cid: Union[int, None, EmptyStrToNone] = Query(default=None)
    is_show: Union[int, EmptyStrToNone] = Field(default=1)  # 是否显示: [0=否, 1=是]
    start_time: Union[date, None, EmptyStrToNone] = Query(None)  # 开始时间
    end_time: Union[date, None, EmptyStrToNone] = Query(None)  # 结束时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleDetailInSchema(BaseModel):
    """
    文章详情
    """

    id: Union[int, None, EmptyStrToNone] = Query(default=None)


class ArticleAddInSchema(BaseModel):
    """
    新增文章
    """

    title: str
    cid: int
    intro: str
    image: str
    summary: str
    author: str
    content: str
    sort: int
    is_show: int = Field(ge=0, le=1)  # 是否显示: [0=否, 1=是]
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleDeleteInSchema(BaseModel):
    """
    文章删除
    """

    id: int


class ArticleChangeInSchema(BaseModel):
    """
    文章修改状态
    """

    id: int


class ArticleEditInSchema(ArticleAddInSchema):
    """
    编辑文章
    """

    id: int
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleSchema(BaseSchema):
    id: int
    cid: int
    title: str
    intro: str
    image: str
    summary: str
    content: str
    author: str
    sort: int
    visit: int  # 浏览
    is_show: int = Field(ge=0, le=1)  # 是否显示: [0=否, 1=是]
    created_time: datetime | None = Field(None)
    updated_time: datetime | None = Field(None)  # 更新时间


class ArticleListOutSchema(BaseSchema):
    """
    文章 列表输出
    """

    id: int
    category: str | None = Field(None)
    title: str
    intro: str
    image: str
    author: str
    sort: int
    visit: int  # 浏览
    is_show: int = Field(ge=0, le=1)  # 是否显示: [0=否, 1=是]
    created_time: datetime|None = Field(None)
    updated_time: datetime|None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleDetailOutSchema(BaseSchema):
    """
    文章 详情输出
    """

    id: int
    cid: int
    title: str
    intro: str
    image: str
    summary: str
    content: str
    author: str
    sort: int
    visit: int  # 浏览
    is_show: int = Field(ge=0, le=1)  # 是否显示: [0=否, 1=是]
    created_time: datetime|None = Field(None)
    updated_time: datetime|None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }
